package com.pzh.vue.service.imp;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzh.vue.common.Constants;
import com.pzh.vue.exception.ServiceException;
import com.pzh.vue.mapper.RoleMapper;
import com.pzh.vue.mapper.RoleMenuMapper;
import com.pzh.vue.mapper.SysUserMapper;
import com.pzh.vue.pojo.Menu;
import com.pzh.vue.pojo.RoleMenu;
import com.pzh.vue.pojo.SysUser;
import com.pzh.vue.pojo.dto.SysUserDto;
import com.pzh.vue.service.IMenuService;
import com.pzh.vue.service.SysUserService;
import com.pzh.vue.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImp extends ServiceImpl<SysUserMapper,SysUser> implements SysUserService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private IMenuService iMenuService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Override
    public SysUserDto login(SysUserDto sysUserDto) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",sysUserDto.getUsername());
        queryWrapper.eq("password",sysUserDto.getPassword());
        SysUser user;
        try {
             user = getOne(queryWrapper);
             //设置token 用于用户校验
             String token = TokenUtils.getToken(user.getId().toString(), user.getPassword());
             sysUserDto.setToken(token);
             String role = user.getRole();//ROLE
             int roleId = roleMapper.findByFlag(role);//根据Role查Id
             List<Integer> menuIds = roleMenuMapper.findByRoleID(roleId);//查出当前用户菜单ids
             List<Menu> menus = iMenuService.findAll();//先查询出所有菜单id在排除
             List<Menu> roleMenu=new ArrayList<>();//保存用户菜单
             //筛选当前用户菜单id
            for (Menu menu : menus) {
                if(menuIds.contains(menu.getId())){
                    roleMenu.add(menu);
                }
                List<Menu> children = menu.getChildren();
                children.removeIf(child -> !menuIds.contains(child.getId()));

            }
            sysUserDto.setMenus(roleMenu);

        }catch (Exception e){
            throw new ServiceException(Constants.CODE_600,"用户名或者密码错误");
         }
            if (user!=null){
                BeanUtil.copyProperties(user,sysUserDto,true);
                //不要暴露密码
                sysUserDto.setPassword("别看了，你找不到的");
                return sysUserDto;
            }
            else {
                throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
            }
        }

    @Override
    public SysUserDto register(SysUserDto sysUserDto) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",sysUserDto.getUsername());
       // queryWrapper.eq("password",sysUserDto.getPassword());
        SysUser user;
        try {
            user = getOne(queryWrapper);
        }catch (Exception e){
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (user!=null){
            throw new ServiceException(Constants.CODE_600,"已有用户");
        }
        else {
            user=new SysUser();
            BeanUtil.copyProperties(sysUserDto,user,true);
            save(user);
            return sysUserDto;
        }
    }

    @Override
    public boolean checkRoot(String username) {
        String root = sysUserMapper.isRoot(username);
        if (root.equals("ROOT")||root.equals("ADMIN")){
            return true;
        }
        return false;
    }

    @Override
    public String selectFile(String username) {
        String file = sysUserMapper.selectFileByName(username);
        return file;
    }

    @Override
    public int updateFileURL(String fileUrl,String username) {
        int i= sysUserMapper.updateFileURL(fileUrl,username);
        return i;
    }

    @Override
    public int updateSelfEvaluation(String selfEvaluation, String username) {
        return sysUserMapper.updateSelfEvaluation(selfEvaluation,username);
    }

    @Override
    public String findScore(String username) {
       return sysUserMapper.findScore(username);
    }

}

